package com.honey.mybatis.generator.criteria.method;

import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.JavaVisibility;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.criteria.CriteriaConstant;

public class AddCriterion2Method extends AbstractMethod {
	
	private static final String VALUE = "value";
	
	private static final String PROPERTY = "property";
	
	
	private static final Parameter []PARAMETER =new Parameter[]{ 
		new Parameter(new FullyQualifiedJavaType("Enum<?>"),"name"),
		new Parameter(new FullyQualifiedJavaType(CriteriaConstant.TERM_CLASS_NAME),CriteriaConstant.TERM_FIELD_NAME),
		//new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),CriteriaConstant.CONDITION_FIELD_NAME),
		new Parameter(JDKFullyQualifiedJavaType.getObjectInstance(),VALUE),
		//new Parameter(JDKFullyQualifiedJavaType.getObjectInstance(),PROPERTY),
	};
	
	private static final String[] JAVA_DOC=new String[]{
		"有一个值的查询,例如: > < 等查查询。"
	};
	
	public AddCriterion2Method(){
	}
	
	public AddCriterion2Method(Clazz clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
		
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		return CriteriaConstant.ADD_CRITERIA_METHOD ;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getParameter()
	 */
	@Override
	protected Parameter[] getParameter() {
		return PARAMETER;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		IfBlock block = new IfBlock(VALUE+" == null");
		block.addBodyLine("throw new RuntimeException(\"Value for \"+name+\" cannot be null\")");
		method.addBlocks(block);
		method.addBodyLine((new StringBuilder()).append(JavaKeyWord.OPERATION_THIS).append(CriteriaConstant.CRITERION_FIELD_NAME).append(".add(new ").append(CriteriaConstant.CRITERION_CLASS_NAME).append("(name, "+CriteriaConstant.TERM_FIELD_NAME+", "+VALUE+")").append(")").toString() );
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodDecoration()
	 */
	@Override
	protected MethodDecoration getMethodDecoration() {
		MethodDecoration answer = super.getMethodDecoration();
		answer.setVisibility(JavaVisibility.PROTECTED);
		return answer;
	}
	
}
